﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using xTeam.HengLe.Entity;
using xTeam.HengLe.DataAccess;
using Valor.Helper;
using xTeam.HengLe.Enums;
using System.Web.Script.Serialization;

namespace xTeam.HengLe.Controller.Manage
{
    public class AdvertiseController : BaseController
    {
        private static AdvertiseDataAccess _advertiseDataAccess;

        public AdvertiseController()
        {
            _advertiseDataAccess = new AdvertiseDataAccess(_connectionString);
        }

        public ActionResult Index(string modulekey = "")
        {

            List<EAdvertise> adList = _advertiseDataAccess.SelectList(string.Empty, modulekey);
            return View(adList);
        }

        public ActionResult Post(int aid = 0, string moduleKey = "Home")
        {
            EAdvertise ad = _advertiseDataAccess.SelectById(aid) ?? new EAdvertise() { ModuleKey = moduleKey };
            List<object> adType = EnumHelper.GetEnumsToList(typeof(AdType));
            if ((AdType)ad.Type == AdType.Slide)
            {
                JavaScriptSerializer json = new JavaScriptSerializer();
                try { ad.SlideList = json.Deserialize<List<ESlide>>(ad.Contents); }
                catch { ad.SlideList = new List<ESlide>(); }
            }
            ViewData["adType"] = new SelectList(adType, "Value", "Text", ad.Type);
            return View(ad);
        }
        [HttpPost]
        public ActionResult Post
            (
            string adTitle = ""
            , int adAid = 0
            , int adType = 0
            , string adKey = ""
            , string adDescription = ""
            , int adSequence = 0
            , string adModuleKey = ""
            , string ckeditor = ""
            )
        {
            if (string.IsNullOrEmpty(adModuleKey))
                throw new System.Web.HttpException("参数错误");
            string contents = string.Empty;
            switch ((AdType)adType)
            {
                //富文本格式
                case AdType.RichText:
                    contents = ckeditor;
                    break;
                // 幻灯片格式
                case AdType.Slide:
                    List<ESlide> slide = new List<ESlide>();
                    if (Request.Form["Title"] != null)
                    {
                        string[] titles = Request.Form.GetValues("Title");
                        string[] redirects = Request.Form.GetValues("redirect");
                        string[] filePaths = Request.Form.GetValues("filePath");
                        string[] sequences = Request.Form.GetValues("sequence");
                        int sequence = 0;
                        for (int i = 0; i < titles.Length; i++)
                        {
                            int.TryParse(sequences[i], out sequence);
                            slide.Add(new ESlide()
                            {
                                Title = titles[i],
                                Redirect = redirects[i],
                                ImageName = filePaths[i],
                                Sequence = sequence
                            });
                        }
                    }
                    JavaScriptSerializer json = new JavaScriptSerializer();
                    contents = json.Serialize(slide);
                    break;
            }

            int result = _advertiseDataAccess.Save(new EAdvertise()
            {
                Type = adType,
                Title = adTitle,
                Sequence = adSequence,
                ModuleKey = adModuleKey,
                ExtendKey = adKey,
                Description = adDescription,
                Contents = contents,
                Aid = adAid
            });

            return RedirectToAction("Post", new { aid = result });
        }
    }
}
